/**
 * Bakalarska prace - Inteligentni budik
 * @package sleepCatcher
 * @file FormAlarmRepeat.cpp
 * @author Jan Pesava - xpesav00
 * @email xpesav00@stud.fit.vutbr.cz
 * @date 24. 12. 2011
 */

#include "forms/FormAlarmRepeat.h"
#include "SCFormManager.h"
#include "SCTranslator.h"


FormAlarmRepeat::FormAlarmRepeat()
{
}

FormAlarmRepeat::~FormAlarmRepeat()
{
}


bool
FormAlarmRepeat::Initialize()
{
	Construct(L"IDF_ALARM_REPEAT");
	return true;
}

result
FormAlarmRepeat::OnInitializing(void)
{
	result r = E_SUCCESS;

	this->pScrollPanel = static_cast<ScrollPanel*>(GetControl("IDC_SCROLLPANEL_REPEAT"));

	this->pOnlyOnce = static_cast<CheckButton*>(pScrollPanel->GetControl("IDC_CHECKBUTTON_ONLYONCE"));
	this->pEveryDay = static_cast<CheckButton*>(pScrollPanel->GetControl("IDC_CHECKBUTTON_EVERYDAY"));
	this->pWeekdays = static_cast<CheckButton*>(pScrollPanel->GetControl("IDC_CHECKBUTTON_WEEKDAYS"));
	this->pWeekend = static_cast<CheckButton*>(pScrollPanel->GetControl("IDC_CHECKBUTTON_WEEKEND"));
	this->pMonday = static_cast<CheckButton*>(pScrollPanel->GetControl("IDC_CHECKBUTTON_MONDAY"));
	this->pTuesday = static_cast<CheckButton*>(pScrollPanel->GetControl("IDC_CHECKBUTTON_TUESDAY"));
	this->pWednesday = static_cast<CheckButton*>(pScrollPanel->GetControl("IDC_CHECKBUTTON_WEDNESDAY"));
	this->pThursday = static_cast<CheckButton*>(pScrollPanel->GetControl("IDC_CHECKBUTTON_THURSDAY"));
	this->pFriday = static_cast<CheckButton*>(pScrollPanel->GetControl("IDC_CHECKBUTTON_FRIDAY"));
	this->pSaturday = static_cast<CheckButton*>(pScrollPanel->GetControl("IDC_CHECKBUTTON_SATURDAY"));
	this->pSunday = static_cast<CheckButton*>(pScrollPanel->GetControl("IDC_CHECKBUTTON_SUNDAY"));

	//language
	this->SetTitleText(SCTranslator::GetText("IDS_ALARMSREPEAT_TITLE"));
	this->pOnlyOnce->SetText(SCTranslator::GetText("IDS_ALARMSREPEAT_ONLYONCE"));
	this->pEveryDay->SetText(SCTranslator::GetText("IDS_ALARMSREPEAT_EVERYDAY"));
	this->pWeekdays->SetText(SCTranslator::GetText("IDS_ALARMSREPEAT_WEEKDAYS"));
	this->pWeekend->SetText(SCTranslator::GetText("IDS_ALARMSREPEAT_WEEKEND"));
	this->pMonday->SetText(SCTranslator::GetText("IDS_GENERAL_MONDAY"));
	this->pTuesday->SetText(SCTranslator::GetText("IDS_GENERAL_TUESDAY"));
	this->pWednesday->SetText(SCTranslator::GetText("IDS_GENERAL_WEDNESDAY"));
	this->pThursday->SetText(SCTranslator::GetText("IDS_GENERAL_THURSDAY"));
	this->pFriday->SetText(SCTranslator::GetText("IDS_GENERAL_FRIDAY"));
	this->pSaturday->SetText(SCTranslator::GetText("IDS_GENERAL_SATURDAY"));
	this->pSunday->SetText(SCTranslator::GetText("IDS_GENERAL_SUNDAY"));

	this->pOnlyOnce->SetActionId(IDA_ONLYONCE_SET, IDA_ONLYONCE_CANCEL);
	this->pEveryDay->SetActionId(IDA_EVERYDAY_SET, IDA_EVERYDAY_CANCEL);
	this->pWeekdays->SetActionId(IDA_WEEKDAYS_SET, IDA_WEEKDAYS_CANCEL);
	this->pWeekend->SetActionId(IDA_WEEKEND_SET, IDA_WEEKEND_CANCEL);
	this->pMonday->SetActionId(IDA_MONDAY_SET, IDA_MONDAY_CANCEL);
	this->pTuesday->SetActionId(IDA_TUESDAY_SET, IDA_TUESDAY_CANCEL);
	this->pWednesday->SetActionId(IDA_WEDNESDAY_SET, IDA_WEDNESDAY_CANCEL);
	this->pThursday->SetActionId(IDA_THURSDAY_SET, IDA_THURSDAY_CANCEL);
	this->pFriday->SetActionId(IDA_FRIDAY_SET, IDA_FRIDAY_CANCEL);
	this->pSaturday->SetActionId(IDA_SATURDAY_SET, IDA_SATURDAY_CANCEL);
	this->pSunday->SetActionId(IDA_SUNDAY_SET, IDA_SUNDAY_CANCEL);

	this->pOnlyOnce->AddActionEventListener(*this);
	this->pEveryDay->AddActionEventListener(*this);
	this->pWeekdays->AddActionEventListener(*this);
	this->pWeekend->AddActionEventListener(*this);
	this->pMonday->AddActionEventListener(*this);
	this->pTuesday->AddActionEventListener(*this);
	this->pWednesday->AddActionEventListener(*this);
	this->pThursday->AddActionEventListener(*this);
	this->pFriday->AddActionEventListener(*this);
	this->pSaturday->AddActionEventListener(*this);
	this->pSunday->AddActionEventListener(*this);

	// Set Footer
	Footer* pFooter = this->GetFooter();

	FooterItem cancelItem;
	cancelItem.Construct(IDA_ALARM_BACK);
	cancelItem.SetText(SCTranslator::GetText("IDS_ALARMS_BACK"));

	pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
	pFooter->AddItem(cancelItem);

	pFooter->AddActionEventListener(*this);

	//this->Update();

	return r;
}

result
FormAlarmRepeat::OnTerminating(void)
{
	result r = E_SUCCESS;
	return r;
}

void
FormAlarmRepeat::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{

	SCAlarmRepeat* pAlarmRepeat = pAlarm->GetAlarmRepeat();
	ArrayList* pParams;

	switch(actionId)
	{
		case IDA_ALARM_BACK:
			this->Cancel();
			pParams = new ArrayList();
			pParams->Add(*this->pAlarm);
			SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM_SET, false, pParams, SCFormManager::SLIDE_RIGHT);
			pParams->RemoveAll();
			delete pParams;
			break;
		case IDA_EVERYDAY_SET:
			pAlarmRepeat->SetEveryDay();
			break;
		case IDA_EVERYDAY_CANCEL:
			pAlarmRepeat->SetOnlyOnce();
			break;
		case IDA_ONLYONCE_SET:
			pAlarmRepeat->SetOnlyOnce();
			break;
		case IDA_ONLYONCE_CANCEL:
			pAlarmRepeat->SetEveryDay();
			break;
		case IDA_WEEKDAYS_SET:
			pAlarmRepeat->SetOnlyWeekDays();
			break;
		case IDA_WEEKDAYS_CANCEL:
			pAlarmRepeat->SetOnlyOnce();
			break;
		case IDA_WEEKEND_SET:
			pAlarmRepeat->SetOnlyWeekend();
			break;
		case IDA_WEEKEND_CANCEL:
			pAlarmRepeat->SetOnlyOnce();
			break;
		case IDA_MONDAY_SET:
			pAlarmRepeat->SetMonday(true);
			break;
		case IDA_MONDAY_CANCEL:
			pAlarmRepeat->SetMonday(false);
			break;
		case IDA_TUESDAY_SET:
			pAlarmRepeat->SetTuesday(true);
			break;
		case IDA_TUESDAY_CANCEL:
			pAlarmRepeat->SetTuesday(false);
			break;
		case IDA_WEDNESDAY_SET:
			pAlarmRepeat->SetWednesday(true);
			break;
		case IDA_WEDNESDAY_CANCEL:
			pAlarmRepeat->SetWednesday(false);
			break;
		case IDA_THURSDAY_SET:
			pAlarmRepeat->SetThursday(true);
			break;
		case IDA_THURSDAY_CANCEL:
			pAlarmRepeat->SetThursday(false);
			break;
		case IDA_FRIDAY_SET:
			pAlarmRepeat->SetFriday(true);
			break;
		case IDA_FRIDAY_CANCEL:
			pAlarmRepeat->SetFriday(false);
			break;
		case IDA_SATURDAY_SET:
			pAlarmRepeat->SetSaturday(true);
			break;
		case IDA_SATURDAY_CANCEL:
			pAlarmRepeat->SetSaturday(false);
			break;
		case IDA_SUNDAY_SET:
			pAlarmRepeat->SetSunday(true);
			break;
		case IDA_SUNDAY_CANCEL:
			pAlarmRepeat->SetSunday(false);
			break;
	}

	if(actionId != IDA_ALARM_BACK)
	{
		this->Redraw();
	}
}

void
FormAlarmRepeat::Update()
{
	if(this->pAlarm != NULL)
	{
		SCAlarmRepeat* pAlarmRepeat = this->pAlarm->GetAlarmRepeat();
		this->pEveryDay->SetSelected(pAlarmRepeat->IsEveryDays());
		this->pOnlyOnce->SetSelected(pAlarmRepeat->IsJustOnlyOnce());
		this->pWeekdays->SetSelected(pAlarmRepeat->IsJustWeekdays());
		this->pWeekend->SetSelected(pAlarmRepeat->IsJustWeekend());
		this->pMonday->SetSelected(pAlarmRepeat->GetMonday());
		this->pTuesday->SetSelected(pAlarmRepeat->GetTuesday());
		this->pWednesday->SetSelected(pAlarmRepeat->GetWednesday());
		this->pThursday->SetSelected(pAlarmRepeat->GetThursday());
		this->pFriday->SetSelected(pAlarmRepeat->GetFriday());
		this->pSaturday->SetSelected(pAlarmRepeat->GetSaturday());
		this->pSunday->SetSelected(pAlarmRepeat->GetSunday());
	}
}

void
FormAlarmRepeat::Redraw()
{
	this->Update();
	RequestRedraw(true);
}

void
FormAlarmRepeat::Cancel()
{
	// Not implemented
}

void
FormAlarmRepeat::Save()
{
	 // Not implemented
}

int
FormAlarmRepeat::GetFormId()
{
	return SCFormManager::IDF_ALARM_REPEAT;
}

void
FormAlarmRepeat::SetAlarm(SCAlarm* pAlarm)
{
	this->pAlarm = pAlarm;
}

SCAlarm*
FormAlarmRepeat::GetAlarm()
{
	return this->pAlarm;
}
